package com.gt.admin.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gt.admin.domain.Check;
import com.gt.admin.domain.CheckGraph;
import com.gt.admin.domain.vo.CheckVO;
import com.gt.admin.mapper.CheckMapper;
import com.gt.admin.mapper.GraphMapper;
import com.gt.admin.service.ICheckService;
import com.gt.admin.utils.GraphChart;
import com.gt.admin.utils.GraphData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CheckServiceImpl extends ServiceImpl<CheckMapper, Check> implements ICheckService {
    @Autowired
    private CheckMapper checkMapper;

    @Autowired
    private GraphMapper checkGraphMapper;
    @Override
    public List<CheckVO> list(CheckVO checkVO){
        String operationList = checkVO.getOperationList();
        if (operationList != "" && operationList != null){
            checkVO.setOperations(operationList.split(","));
        }if (checkVO.getSickId() == null){
            checkVO.setSickId(0L);
        }return checkMapper.list(checkVO);
    }

    /**
     * 分析，返回图表数据
     * @param checkVO
     * @return
     */
    @Override
    public List<com.gt.admin.utils.Graph> analyse(CheckVO checkVO) {
        // 筛查目录id为空时 默认为0
        if (checkVO.getSickId() == null){
            checkVO.setSickId(0L);
        }
        // 分析图表
        List<com.gt.admin.utils.Graph> graphList = new ArrayList<>();
        graphList.add(new com.gt.admin.utils.Graph(GraphChart.BAR_COL, getGraphDatas(checkVO,GraphChart.BAR_COL)));
        graphList.add(new com.gt.admin.utils.Graph(GraphChart.BAR_ROW, getGraphDatas(checkVO,GraphChart.BAR_ROW)));
        graphList.add(new com.gt.admin.utils.Graph(GraphChart.PIE, getGraphDatas(checkVO,GraphChart.PIE)));
        graphList.add(new com.gt.admin.utils.Graph(GraphChart.BROKEN_LINE, getGraphDatas(checkVO,GraphChart.BROKEN_LINE)));
        return graphList;
    }

    /**
     * 获取表单数据，          待实现
     * @param checkVVO
     * @return
     */
    @Override
    public List<GraphData> getGraphDatas(CheckVO checkVVO,String chart) {
//        GraphData data1 = new GraphData();
//        GraphData data2 = new GraphData();
//        GraphData data3 = new GraphData();
//        GraphData data4 = new GraphData();
        List<GraphData> dataList = new ArrayList<>();
        List<CheckGraph> graphs = checkGraphMapper.selectList(Wrappers.<CheckGraph>lambdaQuery()
                .eq(CheckGraph::getAnalyseModel, checkVVO.getAnalyseModel())
                .eq(CheckGraph::getType,GraphChart.CHECK_GRAPH));


        for (CheckGraph graph : graphs) {
            if (chart.equals(graph.getChart())){
                GraphData data = new GraphData();
                data.setLabel(graph.getXCoordinate());
                data.setValue(graph.getYCoordinate());
                dataList.add(data);
            }

        }
//        data1.setLabel("doctor");
//        data1.setValue(13);
//        data2.setLabel("patient");
//        data2.setValue(66);
//        data3.setLabel("aaa");
//        data3.setValue(4);
//        data4.setLabel("数据");
//        data4.setValue(8);
//
//        dataList.add(data1);
//        dataList.add(data2);
//        dataList.add(data3);
//        dataList.add(data4);
        return dataList;
    }
}
